﻿using System;
using System.Configuration;
using Dentiste.Domain.Factories;
using Dentiste.Domain.Interfaces;
using Dentiste.Domain.Providers;

namespace Dentiste.Domain
{
    public class DentisteManager
    {
        private static DentisteManager _instance;
        private readonly InamiProvider _inamiProvider;
        private readonly InvoiceProvider _invoiceProvider;
        private readonly PatientProvider _patientProvider;
        private readonly RowProvider _rowProvider;

        public DentisteManager()
        {
            string connectionString =
                ConfigurationManager.ConnectionStrings["Dentiste.Domain.Properties.Settings.DatabaseConnectionString"]
                    .ConnectionString;

            _inamiProvider = new InamiProvider(connectionString);
            _rowProvider = new RowProvider(connectionString, _inamiProvider);
            _patientProvider = new PatientProvider(connectionString);
            _invoiceProvider = new InvoiceProvider(connectionString, _patientProvider, _rowProvider);
        }

        //public DbSetCustom<IInami> Inamis { get; private set; }
        //public DbSetCustom<IRow> Rows { get; private set; }
        //public DbSetCustom<IPatient> Patients { get; private set; }
        //public DbSetCustom<IInvoice> Invoices { get; private set; }

        public static DentisteManager Instance
        {
            get { return _instance ?? (_instance = new DentisteManager()); }
        }

        #region Inami

        public DbSetCustom<IInami> SelectAllInami()
        {
            return _inamiProvider.Select();
        }

        public IInami SelectInami(string id)
        {
            return _inamiProvider.Select(id);
        }

        public IInami InsertInami(IInami i)
        {
            return _inamiProvider.Insert(i);
        }

        public bool UpdateInami(IInami i)
        {
            return _inamiProvider.Update(i);
        }

        public bool DeleteInami(IInami i)
        {
            return _inamiProvider.Delete(i);
        }

        public void Link(IInami iParent, IInami iChild)
        {
            _inamiProvider.LinkRequiredInami(iParent, iChild);
        }

        public void Unlink(IInami iParent, IInami iChild)
        {
            _inamiProvider.UnlinkRequiredInami(iParent, iChild);
        }

        #endregion

        #region Row

        public DbSetCustom<IRow> SelectAllRows()
        {
            return _rowProvider.Select();
        }

        public IRow SelectRow(string id)
        {
            return _rowProvider.Select(id);
        }

        public IRow SelectRow(int id)
        {
            return _rowProvider.Select(Convert.ToString(id));
        }

        public IRow InsertRow(IRow r)
        {
            return _rowProvider.Insert(r);
        }

        public bool UpdateRow(IRow r)
        {
            return _rowProvider.Update(r);
        }

        public bool DeleteRow(IRow r)
        {
            return _rowProvider.Delete(r);
        }

        #endregion

        #region Patient

        public DbSetCustom<IPatient> SelectAllPatient()
        {
            return _patientProvider.Select();
        }

        public IPatient SelectPatient(string id)
        {
            return _patientProvider.Select(id);
        }

        public IPatient SelectPatient(string firstname, string lastname)
        {
            return _patientProvider.Select(firstname, lastname);
        }

        public IPatient SelectPatient(int id)
        {
            return _patientProvider.Select(Convert.ToString(id));
        }

        public IPatient InsertPatient(IPatient p)
        {
            return _patientProvider.Insert(p);
        }

        public bool UpdatePatient(IPatient p)
        {
            return _patientProvider.Update(p);
        }

        public bool DeletePatient(IPatient p)
        {
            return _patientProvider.Delete(p);
        }

        #endregion

        #region Invoice

        public DbSetCustom<IInvoice> SelectAllInvoice()
        {
            return _invoiceProvider.Select();
        }

        public IInvoice SelectInvoice(string id)
        {
            return _invoiceProvider.Select(id);
        }

        public IInvoice SelectInvoice(int id)
        {
            return _invoiceProvider.Select(Convert.ToString(id));
        }

        public IInvoice InsertInvoice(IInvoice i)
        {
            return _invoiceProvider.Insert(i);
        }

        public bool UpdateInvoice(IInvoice i)
        {
            return _invoiceProvider.Update(i);
        }

        public bool DeleteInvoice(IInvoice i)
        {
            return _invoiceProvider.Delete(i);
        }

        #endregion

    }
}